new Vue({
    el: "#container",
    data() {
        return {
            //  生产方式
            tabIndentPro: 0,
            //  浇筑方式
            castModels: [],
            //  任务单浇筑方式
            tabIndentCast: -1,
            // 查询数据
            tableSearch: {
                dateFormat: "yyyy-MM-dd HH:mm",
                //  任务单生产状态
                proType: null,
            },
            //  显示的生产线数据
            proLines: [],
            //  排队车辆
            queueCars: [],
            //  生产完成的发货单
            completedInvs: [],
            //  运输车辆
            tranInvs: [],
            //  其他车辆
            otherCars: [],
            workIndentLoading: false,
            workIndents: [],
            //  当前激活的任务单
            activeIndentCard: null,
            //  发货单详情
            invoiceDetail: {},
            //  生产线的车辆
            disPopover: false,
            //  全部车辆列表
            allCarList: [],

            //  退砼记录
            invBackList: [],
            invBakPopover: false,
            //  表数据
            invoiceDatas: [],
            //  搜索车辆熟悉
            carSearch: {
                queue: null,
                other: null,
            },
            //  详情数据
            detailDialog: {
                show: false,
                invoiceId: false,
            },
            //  近10车
            nearlyInvList: [],
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadWorkIndents();
        that.loadBaseDatas();
        that.refreshDatas();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        //  刷新数据方法
        refreshDatas() {
            let that = this;
            that.loadProLines();
            that.loadCarList();
            //  加载发货单
            that.loadDisInvPage();
            //  加载近10车
            that.loadNearlyInvList();
            //  加载方量
            that.loadAccuAndTran();
            setTimeout(function () {
                that.refreshDatas();
            }, 30000);
        },
        //  加载各个基础数据
        loadBaseDatas() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/base/datas",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let map = res.data;
                        that.roadTypeList = map.roadTypeList;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询基础数据失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        //  加载生产线信息
        loadProLines() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/dispatch/line/producing",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  此处的数据是加载的派单数据
                        that.proLines = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询排队的车辆列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求排队的车辆列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  加载任务单
        loadWorkIndents() {
            let that = this;
            that.invoiceDatas = [];
            $.ajax({
                type: "POST",
                url: "/pro/indent/work/list",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let workIndents = res.data;
                        that.allWorkIndents = workIndents;
                        that.workIndents = workIndents;
                        //  根据任务单浇筑方式分组
                        let castS = [];
                        for (let i = 0, s = workIndents.length; i < s; i++) {
                            let indent = workIndents[i];
                            let castMn = indent.castModelName;
                            if (!(castS.indexOf(castMn) > -1)) {
                                //  没有数据
                                castS.push(castMn);
                            }
                        }
                        castS.sort((a, b) => b.localeCompare(a));
                        that.tabIndentCast = -1;
                        that.castModels = castS;
                        // that.tableSearch.total = page.total;
                        that.loadAccuAndTran();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询任务单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.workIndentLoading = false;
                }
            })
        },
        /**
         * 激活任务单
         */
        changeActiveIndent(index, indent) {
            let that = this;
            that.activeIndentCard = index;
        },
        /**
         * 加载发货单详细信息
         */
        loadInvDetail(inv, column, event) {
            let that = this;
            if (isNull(inv)) {
                return;
            }
            let invoiceId = inv.invoiceId;
            if (isNull(invoiceId)) {
                return;
            }
            that.detailDialog.show = true;
            that.detailDialog.invoiceId = invoiceId;
        },
        loadInvMiniDetail(invId) {
            let that = this;
            if (isNull(invId)) {
                return;
            }
            that.proLineCarLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/overview/" + invId,
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.invoiceDetail = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询发货单详情失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.proLineCarLoading = false;
                }
            });
        },
        /**
         * 列表的生产状态筛选
         * @param proType
         */
        intsTypeChange(proType) {
            let that = this;
            if (proType == 1) {
                that.tableSearch.proType = 1;
            } else if (proType == 3) {
                that.tableSearch.proType = 3;
            } else {
                that.tableSearch.proType = null;
            }
            that.loadWorkIndents();
        },
        /**
         * 待供任务单列表
         */
        showWaitIndent() {
            let that = this;
            that.waitIndentDialog.show = true;
            that.loadWaitIndents();
        },
        //  加载车辆
        loadCarList() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/tran/car/list",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let allCarList = res.data;
                        that.allCarList = allCarList;
                        //  分组
                        let queueCars = [];
                        let otherCars = [];
                        for (let i = 0, s = allCarList.length; i < s; i++) {
                            let car = allCarList[i];
                            if (car.carStatus == 1) {
                                //  排队中
                                let qs = that.carSearch.queue;
                                if (!isNull(qs)) {
                                    //  只匹配筛选数据
                                    if (!isNull(car.code) && car.code.indexOf(qs) > -1) {
                                        queueCars.push(car);
                                    } else if (!isNull(car.carNo) && car.carNo.indexOf(qs) > -1) {
                                        queueCars.push(car);
                                    }
                                } else {
                                    queueCars.push(car);
                                }
                            } else if (car.carStatus != 2) {
                                //  不在排队也不在生产中
                                let qs = that.carSearch.other;
                                if (!isNull(qs)) {
                                    //  只匹配筛选数据
                                    if (!isNull(car.code) && car.code.indexOf(qs) > -1) {
                                        otherCars.push(car);
                                    } else if (!isNull(car.carNo) && car.carNo.indexOf(qs) > -1) {
                                        otherCars.push(car);
                                    }
                                } else {
                                    otherCars.push(car);
                                }
                            }
                        }
                        that.queueCars = queueCars;
                        that.otherCars = otherCars;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询车辆列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求车辆列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  取消弹窗
        cancelInvDisForm() {
            let that = this;
            that.addInvDispatchForm = {
                //  发货单
                invoiceId: null,
                lineId: null,
                stere: null,
            }
            that.addInvDispatchDialog.show = false;
        },
        // 加载数据
        loadDisInvPage: function () {
            let that = this;
            //that.invoiceLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/dis/page",
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.invoiceDatas = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    //that.invoiceLoading = false;
                }
            })
        },
        //  修改数据
        castModelChange(cast) {
            let that = this;
            //  筛选数据
            let indetList = [];
            //  全部数据
            let allIndents = that.allWorkIndents;
            if (cast == -1) {
                that.workIndents = allIndents;
                return;
            }
            for (let i = 0, s = allIndents.length; i < s; i++) {
                let ind = allIndents[i];
                if (cast == ind.castModelName) {
                    indetList.push(ind);
                }
            }
            that.workIndents = indetList;
        },
        // 加载近10车数据
        loadNearlyInvList: function () {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/nearly/list",
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.nearlyInvList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  查询任务单的累计方量和运输车辆
        loadAccuAndTran() {
            let that = this;
            let workIndents = that.workIndents;
            if (workIndents != null && workIndents.length > 0)
                for (let i = 0, s = workIndents.length; i < s; i++) {
                    let workIndent = workIndents[i];
                    let indentId = workIndent.id;
                    //  加载车辆数据
                    $.ajax({
                        type: "POST",
                        url: "/pro/invoice/accuAndTran",
                        async: true,
                        dataType: 'json',
                        data: {
                            indentId: indentId,
                        },
                        success: function (res) {
                            if (res.code == 0) {
                                let aat = res.data;
                                if (aat != null) {
                                    let aat = res.data;
                                    let totalCar = aat.totalCar;
                                    let sendStere = aat.sendStere;
                                    let carCodeList = aat.carCodeList;
                                    workIndent.totalCar = totalCar;
                                    workIndent.sendStere = sendStere;
                                    workIndent.carCodeList = carCodeList;
                                }
                            } else {

                            }
                        },
                        error: function (res) {
                            return;
                        },
                        complete: function (XMLHttpRequest, status) {
                        }
                    })
                }
        }
    }
})
